import 'babel-polyfill'
import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'
import 'element-ui/lib/theme-chalk/index.css'
import '@/elementui.js'
// import Avatar from './components/avatar.vue'
import { message } from '@/libs/message.js'
import { showLoading, hideLoading } from '@/libs/loading.js'
import i18n from './lang'
import vueQuillEditor from 'vue-quill-editor'
import 'quill/dist/quill.snow.css'
// import tim from './tim'
// import TIM from 'tim-js-sdk'
// import './assets/icon/iconfont.css'
// import './assets/icon/tim.css'
import adaptive from '@/libs/el-table'

Vue.config.productionTip = false

// window.tim = tim
// window.TIM = TIM
Vue.prototype.$bus = new Vue()
// Vue.prototype.tim = tim
// Vue.prototype.TIM = TIM
Vue.prototype.$message = message
Vue.prototype.$showLoading = showLoading
Vue.prototype.$hideLoading = hideLoading

Vue.use(vueQuillEditor)
Vue.use(adaptive)

// 设置点击间隔
Vue.directive('preventReClick', {
  inserted(el) {
    el.addEventListener('click', () => {
      if (!el.disabled) {
        el.disabled = true
        setTimeout(() => {
          el.disabled = false
        }, 1500)
      }
    })
  }
})

router.beforeEach((to, from, next) => {
  // 是否直接访问login页
  if (to.path === '/login') return next()
  // 是否含有token
  if (!store.getters.getToken)  return next('/login')
  // 动态添加title
  document.getElementById('public_title').innerHTML = to.meta.pagetitle === undefined ? '' : to.meta.pagetitle
  let isSec = to.path.indexOf('sec')
  if (to.path !== '/login' && to.path !== '/home' && to.path !== '/' && to.path !== '/404' && isSec == -1) {
    let roleMenu = []
    if (sessionStorage.getItem('roleMenu')) roleMenu = JSON.parse(sessionStorage.getItem('roleMenu'))
    let isRole = roleMenu.findIndex(item => {
      return to.path === item.url
    })
    if (isRole == -1) return next('/404')
  }
  // 所有验证通过,放行
  next()
})

// Vue.component('avatar', Avatar)

window.vm = new Vue({
  router,
  store,
  i18n,
  render: h => h(App)
}).$mount('#app')
